package chartCraft.command;

import java.awt.Toolkit;
import java.util.Observable;
import java.util.Stack;

import chartCraft.Log;
import chartCraft.SaveStateManager;


public class CommandBroker extends Observable{
	
	private static final char BELL = 7;
	private final Stack<Command> doneStack = new Stack<Command>();
	private final Stack<Command> redoStack = new Stack<Command>();
	private SaveStateManager mSaveState;

	
	public CommandBroker(SaveStateManager saveState) {
		mSaveState = saveState;
	}

	public void executeNewCommand(Command cmd) {
		cmd.doIt();
		doneStack.add(cmd);
		redoStack.clear();
		
		mSaveState.setDirty(true);
		
		setChanged();
		notifyObservers();
	}
	
	public void undo() {
		if (!doneStack.empty()) {
			Command cmd = doneStack.pop();
			redoStack.push(cmd);
			cmd.undoIt();
			
			mSaveState.setDirty(true);
		} else {
			Toolkit.getDefaultToolkit().beep();     
		}
		
		setChanged();
		notifyObservers();
	}
	
	public void redo() {
		if (!redoStack.empty()) {
			Command cmd = redoStack.pop();
			doneStack.push(cmd);
			cmd.doIt();
			
			mSaveState.setDirty(true);
		} else {
			Toolkit.getDefaultToolkit().beep();
		}	
		
		setChanged();
		notifyObservers();
	}
	
	public boolean canUndo() {
		return !doneStack.empty();
	}
	
	public boolean canRedo() {
		return !redoStack.empty();
	}

	public static interface Command {
		public void doIt();	
		public void undoIt();
	}





}




